home *** CD-ROM | disk | FTP | other *** search
/ Hardcore Visual Basic 5.0 (2nd Edition) / Hardcore Visual Basic 5.0 - Second Edition (1997)(Microsoft Press).iso / Code / comdlg.bas < prev    next >
BASIC Source File  |  1997-06-14  |  32KB  |  919 lines

  1. Attribute VB_Name = "MCommonDialog"
  2. Option Explicit
  3.  
  4. Public Enum EErrorCommonDialog
  5.     eeBaseCommonDialog = 13450  ' CommonDialog
  6. End Enum
  7.  
  8. Private Type OPENFILENAME
  9.     lStructSize As Long          ' Filled with UDT size
  10.     hwndOwner As Long            ' Tied to Owner
  11.     hInstance As Long            ' Ignored (used only by templates)
  12.     lpstrFilter As String        ' Tied to Filter
  13.     lpstrCustomFilter As String  ' Ignored (exercise for reader)
  14.     nMaxCustFilter As Long       ' Ignored (exercise for reader)
  15.     nFilterIndex As Long         ' Tied to FilterIndex
  16.     lpstrFile As String          ' Tied to FileName
  17.     nMaxFile As Long             ' Handled internally
  18.     lpstrFileTitle As String     ' Tied to FileTitle
  19.     nMaxFileTitle As Long        ' Handled internally
  20.     lpstrInitialDir As String    ' Tied to InitDir
  21.     lpstrTitle As String         ' Tied to DlgTitle
  22.     Flags As Long                ' Tied to Flags
  23.     nFileOffset As Integer       ' Ignored (exercise for reader)
  24.     nFileExtension As Integer    ' Ignored (exercise for reader)
  25.     lpstrDefExt As String        ' Tied to DefaultExt
  26.     lCustData As Long            ' Ignored (needed for hooks)
  27.     lpfnHook As Long             ' Ignored (good luck with hooks)
  28.     lpTemplateName As Long       ' Ignored (good luck with templates)
  29. End Type
  30.  
  31. Private Declare Function GetOpenFileName Lib "COMDLG32" _
  32.     Alias "GetOpenFileNameA" (file As OPENFILENAME) As Long
  33. Private Declare Function GetSaveFileName Lib "COMDLG32" _
  34.     Alias "GetSaveFileNameA" (file As OPENFILENAME) As Long
  35. Private Declare Function GetFileTitle Lib "COMDLG32" _
  36.     Alias "GetFileTitleA" (ByVal szFile As String, _
  37.     ByVal szTitle As String, ByVal cbBuf As Long) As Long
  38.  
  39. Public Enum EOpenFile
  40.     OFN_READONLY = &H1
  41.     OFN_OVERWRITEPROMPT = &H2
  42.     OFN_HIDEREADONLY = &H4
  43.     OFN_NOCHANGEDIR = &H8
  44.     OFN_SHOWHELP = &H10
  45.     OFN_ENABLEHOOK = &H20
  46.     OFN_ENABLETEMPLATE = &H40
  47.     OFN_ENABLETEMPLATEHANDLE = &H80
  48.     OFN_NOVALIDATE = &H100
  49.     OFN_ALLOWMULTISELECT = &H200
  50.     OFN_EXTENSIONDIFFERENT = &H400
  51.     OFN_PATHMUSTEXIST = &H800
  52.     OFN_FILEMUSTEXIST = &H1000
  53.     OFN_CREATEPROMPT = &H2000
  54.     OFN_SHAREAWARE = &H4000
  55.     OFN_NOREADONLYRETURN = &H8000
  56.     OFN_NOTESTFILECREATE = &H10000
  57.     OFN_NONETWORKBUTTON = &H20000
  58.     OFN_NOLONGNAMES = &H40000
  59.     OFN_EXPLORER = &H80000
  60.     OFN_NODEREFERENCELINKS = &H100000
  61.     OFN_LONGNAMES = &H200000
  62. End Enum
  63.  
  64. Private Type TCHOOSECOLOR
  65.     lStructSize As Long
  66.     hwndOwner As Long
  67.     hInstance As Long
  68.     rgbResult As Long
  69.     lpCustColors As Long
  70.     Flags As Long
  71.     lCustData As Long
  72.     lpfnHook As Long
  73.     lpTemplateName As Long
  74. End Type
  75.  
  76. Private Declare Function ChooseColor Lib "COMDLG32.DLL" _
  77.     Alias "ChooseColorA" (Color As TCHOOSECOLOR) As Long
  78.  
  79. Public Enum EChooseColor
  80.     CC_RGBInit = &H1
  81.     CC_FullOpen = &H2
  82.     CC_PreventFullOpen = &H4
  83.     CC_ColorShowHelp = &H8
  84. ' Win95 only
  85.     CC_SolidColor = &H80
  86.     CC_AnyColor = &H100
  87. ' End Win95 only
  88.     CC_ENABLEHOOK = &H10
  89.     CC_ENABLETEMPLATE = &H20
  90.     CC_EnableTemplateHandle = &H40
  91. End Enum
  92.  
  93. Private Type TCHOOSEFONT
  94.     lStructSize As Long         ' Filled with UDT size
  95.     hwndOwner As Long           ' Caller's window handle
  96.     hDC As Long                 ' Printer DC/IC or NULL
  97.     lpLogFont As Long           ' Pointer to LOGFONT
  98.     iPointSize As Long          ' 10 * size in points of font
  99.     Flags As Long               ' Type flags
  100.     rgbColors As Long           ' Returned text color
  101.     lCustData As Long           ' Data passed to hook function
  102.     lpfnHook As Long            ' Pointer to hook function
  103.     lpTemplateName As Long      ' Custom template name
  104.     hInstance As Long           ' Instance handle for template
  105.     lpszStyle As String         ' Return style field
  106.     nFontType As Integer        ' Font type bits
  107.     iAlign As Integer           ' Filler
  108.     nSizeMin As Long            ' Minimum point size allowed
  109.     nSizeMax As Long            ' Maximum point size allowed
  110. End Type
  111.  
  112. Private Declare Function ChooseFont Lib "COMDLG32" _
  113.     Alias "ChooseFontA" (chfont As TCHOOSEFONT) As Long
  114.  
  115. Public Enum EChooseFont
  116.     CF_ScreenFonts = &H1
  117.     CF_PrinterFonts = &H2
  118.     CF_BOTH = &H3
  119.     CF_FontShowHelp = &H4
  120.     CF_UseStyle = &H80
  121.     CF_EFFECTS = &H100
  122.     CF_AnsiOnly = &H400
  123.     CF_NoVectorFonts = &H800
  124.     CF_NoOemFonts = CF_NoVectorFonts
  125.     CF_NoSimulations = &H1000
  126.     CF_LimitSize = &H2000
  127.     CF_FixedPitchOnly = &H4000
  128.     CF_WYSIWYG = &H8000  ' Must also have ScreenFonts And PrinterFonts
  129.     CF_ForceFontExist = &H10000
  130.     CF_ScalableOnly = &H20000
  131.     CF_TTOnly = &H40000
  132.     CF_NoFaceSel = &H80000
  133.     CF_NoStyleSel = &H100000
  134.     CF_NoSizeSel = &H200000
  135.     ' Win95 only
  136.     CF_SelectScript = &H400000
  137.     CF_NoScriptSel = &H800000
  138.     CF_NoVertFonts = &H1000000
  139.  
  140.     CF_InitToLogFontStruct = &H40
  141.     CF_Apply = &H200
  142.     CF_EnableHook = &H8
  143.     CF_EnableTemplate = &H10
  144.     CF_EnableTemplateHandle = &H20
  145.     CF_FontNotSupported = &H238
  146. End Enum
  147.  
  148. ' These are extra nFontType bits that are added to what is returned to the
  149. ' EnumFonts callback routine
  150.  
  151. Public Enum EFontType
  152.     Simulated_FontType = &H8000
  153.     Printer_FontType = &H4000
  154.     Screen_FontType = &H2000
  155.     Bold_FontType = &H100
  156.     Italic_FontType = &H200
  157.     Regular_FontType = &H400
  158. End Enum
  159.  
  160. Private Type TPRINTDLG
  161.     lStructSize As Long
  162.     hwndOwner As Long
  163.     hDevMode As Long
  164.     hDevNames As Long
  165.     hDC As Long
  166.     Flags As Long
  167.     nFromPage As Integer
  168.     nToPage As Integer
  169.     nMinPage As Integer
  170.     nMaxPage As Integer
  171.     nCopies As Integer
  172.     hInstance As Long
  173.     lCustData As Long
  174.     lpfnPrintHook As Long
  175.     lpfnSetupHook As Long
  176.     lpPrintTemplateName As Long
  177.     lpSetupTemplateName As Long
  178.     hPrintTemplate As Long
  179.     hSetupTemplate As Long
  180. End Type
  181.  
  182. '  DEVMODE collation selections
  183. Private Const DMCOLLATE_FALSE = 0
  184. Private Const DMCOLLATE_TRUE = 1
  185.  
  186. Private Declare Function PrintDlg Lib "COMDLG32.DLL" _
  187.     Alias "PrintDlgA" (prtdlg As TPRINTDLG) As Integer
  188.  
  189. Public Enum EPrintDialog
  190.     PD_ALLPAGES = &H0
  191.     PD_SELECTION = &H1
  192.     PD_PAGENUMS = &H2
  193.     PD_NOSELECTION = &H4
  194.     PD_NOPAGENUMS = &H8
  195.     PD_COLLATE = &H10
  196.     PD_PRINTTOFILE = &H20
  197.     PD_PRINTSETUP = &H40
  198.     PD_NOWARNING = &H80
  199.     PD_RETURNDC = &H100
  200.     PD_RETURNIC = &H200
  201.     PD_RETURNDEFAULT = &H400
  202.     PD_SHOWHELP = &H800
  203.     PD_ENABLEPRINTHOOK = &H1000
  204.     PD_ENABLESETUPHOOK = &H2000
  205.     PD_ENABLEPRINTTEMPLATE = &H4000
  206.     PD_ENABLESETUPTEMPLATE = &H8000
  207.     PD_ENABLEPRINTTEMPLATEHANDLE = &H10000
  208.     PD_ENABLESETUPTEMPLATEHANDLE = &H20000
  209.     PD_USEDEVMODECOPIES = &H40000
  210.     PD_USEDEVMODECOPIESANDCOLLATE = &H40000
  211.     PD_DISABLEPRINTTOFILE = &H80000
  212.     PD_HIDEPRINTTOFILE = &H100000
  213.     PD_NONETWORKBUTTON = &H200000
  214. End Enum
  215.  
  216. Private Type DEVNAMES
  217.     wDriverOffset As Integer
  218.     wDeviceOffset As Integer
  219.     wOutputOffset As Integer
  220.     wDefault As Integer
  221. End Type
  222.  
  223. ' New Win95 Page Setup dialogs are up to you
  224.  
  225. Private Type TPAGESETUPDLG
  226.     lStructSize                 As Long
  227.     hwndOwner                   As Long
  228.     hDevMode                    As Long
  229.     hDevNames                   As Long
  230.     Flags                       As Long
  231.     ptPaperSize                 As POINTL
  232.     rtMinMargin                 As RECT
  233.     rtMargin                    As RECT
  234.     hInstance                   As Long
  235.     lCustData                   As Long
  236.     lpfnPageSetupHook           As Long
  237.     lpfnPagePaintHook           As Long
  238.     lpPageSetupTemplateName     As Long
  239.     hPageSetupTemplate          As Long
  240. End Type
  241.  
  242. ' EPaperSize constants same as vbPRPS constants
  243. Public Enum EPaperSize
  244.     epsLetter = 1          ' Letter, 8 1/2 x 11 in.
  245.     epsLetterSmall         ' Letter Small, 8 1/2 x 11 in.
  246.     epsTabloid             ' Tabloid, 11 x 17 in.
  247.     epsLedger              ' Ledger, 17 x 11 in.
  248.     epsLegal               ' Legal, 8 1/2 x 14 in.
  249.     epsStatement           ' Statement, 5 1/2 x 8 1/2 in.
  250.     epsExecutive           ' Executive, 7 1/2 x 10 1/2 in.
  251.     epsA3                  ' A3, 297 x 420 mm
  252.     epsA4                  ' A4, 210 x 297 mm
  253.     epsA4Small             ' A4 Small, 210 x 297 mm
  254.     epsA5                  ' A5, 148 x 210 mm
  255.     epsB4                  ' B4, 250 x 354 mm
  256.     epsB5                  ' B5, 182 x 257 mm
  257.     epsFolio               ' Folio, 8 1/2 x 13 in.
  258.     epsQuarto              ' Quarto, 215 x 275 mm
  259.     eps10x14               ' 10 x 14 in.
  260.     eps11x17               ' 11 x 17 in.
  261.     epsNote                ' Note, 8 1/2 x 11 in.
  262.     epsEnv9                ' Envelope #9, 3 7/8 x 8 7/8 in.
  263.     epsEnv10               ' Envelope #10, 4 1/8 x 9 1/2 in.
  264.     epsEnv11               ' Envelope #11, 4 1/2 x 10 3/8 in.
  265.     epsEnv12               ' Envelope #12, 4 1/2 x 11 in.
  266.     epsEnv14               ' Envelope #14, 5 x 11 1/2 in.
  267.     epsCSheet              ' C size sheet
  268.     epsDSheet              ' D size sheet
  269.     epsESheet              ' E size sheet
  270.     epsEnvDL               ' Envelope DL, 110 x 220 mm
  271.     epsEnvC3               ' Envelope C3, 324 x 458 mm
  272.     epsEnvC4               ' Envelope C4, 229 x 324 mm
  273.     epsEnvC5               ' Envelope C5, 162 x 229 mm
  274.     epsEnvC6               ' Envelope C6, 114 x 162 mm
  275.     epsEnvC65              ' Envelope C65, 114 x 229 mm
  276.     epsEnvB4               ' Envelope B4, 250 x 353 mm
  277.     epsEnvB5               ' Envelope B5, 176 x 250 mm
  278.     epsEnvB6               ' Envelope B6, 176 x 125 mm
  279.     epsEnvItaly            ' Envelope, 110 x 230 mm
  280.     epsenvmonarch          ' Envelope Monarch, 3 7/8 x 7 1/2 in.
  281.     epsEnvPersonal         ' Envelope, 3 5/8 x 6 1/2 in.
  282.     epsFanfoldUS           ' U.S. Standard Fanfold, 14 7/8 x 11 in.
  283.     epsFanfoldStdGerman    ' German Standard Fanfold, 8 1/2 x 12 in.
  284.     epsFanfoldLglGerman    ' German Legal Fanfold, 8 1/2 x 13 in.
  285.     epsUser = 256          ' User-defined
  286. End Enum
  287.  
  288. ' EPrintQuality constants same as vbPRPQ constants
  289. Public Enum EPrintQuality
  290.     epqDraft = -1
  291.     epqLow = -2
  292.     epqMedium = -3
  293.     epqHigh = -4
  294. End Enum
  295.  
  296. Public Enum EOrientation
  297.     eoPortrait = 1
  298.     eoLandscape
  299. End Enum
  300.  
  301. Private Declare Function PageSetupDlg Lib "COMDLG32" _
  302.     Alias "PageSetupDlgA" (lppage As TPAGESETUPDLG) As Boolean
  303.  
  304. Public Enum EPageSetup
  305.     PSD_Defaultminmargins = &H0 ' Default (printer's)
  306.     PSD_InWinIniIntlMeasure = &H0
  307.     PSD_MINMARGINS = &H1
  308.     PSD_MARGINS = &H2
  309.     PSD_INTHOUSANDTHSOFINCHES = &H4
  310.     PSD_INHUNDREDTHSOFMILLIMETERS = &H8
  311.     PSD_DISABLEMARGINS = &H10
  312.     PSD_DISABLEPRINTER = &H20
  313.     PSD_NoWarning = &H80
  314.     PSD_DISABLEORIENTATION = &H100
  315.     PSD_ReturnDefault = &H400
  316.     PSD_DISABLEPAPER = &H200
  317.     PSD_ShowHelp = &H800
  318.     PSD_EnablePageSetupHook = &H2000
  319.     PSD_EnablePageSetupTemplate = &H8000
  320.     PSD_EnablePageSetupTemplateHandle = &H20000
  321.     PSD_EnablePagePaintHook = &H40000
  322.     PSD_DisablePagePainting = &H80000
  323. End Enum
  324.  
  325. Public Enum EPageSetupUnits
  326.     epsuInches
  327.     epsuMillimeters
  328. End Enum
  329.  
  330. ' Common dialog errors
  331.  
  332. Private Declare Function CommDlgExtendedError Lib "COMDLG32" () As Long
  333.  
  334. Public Enum EDialogError
  335.     CDERR_DIALOGFAILURE = &HFFFF
  336.  
  337.     CDERR_GENERALCODES = &H0
  338.     CDERR_STRUCTSIZE = &H1
  339.     CDERR_INITIALIZATION = &H2
  340.     CDERR_NOTEMPLATE = &H3
  341.     CDERR_NOHINSTANCE = &H4
  342.     CDERR_LOADSTRFAILURE = &H5
  343.     CDERR_FINDRESFAILURE = &H6
  344.     CDERR_LOADRESFAILURE = &H7
  345.     CDERR_LOCKRESFAILURE = &H8
  346.     CDERR_MEMALLOCFAILURE = &H9
  347.     CDERR_MEMLOCKFAILURE = &HA
  348.     CDERR_NOHOOK = &HB
  349.     CDERR_REGISTERMSGFAIL = &HC
  350.  
  351.     PDERR_PRINTERCODES = &H1000
  352.     PDERR_SETUPFAILURE = &H1001
  353.     PDERR_PARSEFAILURE = &H1002
  354.     PDERR_RETDEFFAILURE = &H1003
  355.     PDERR_LOADDRVFAILURE = &H1004
  356.     PDERR_GETDEVMODEFAIL = &H1005
  357.     PDERR_INITFAILURE = &H1006
  358.     PDERR_NODEVICES = &H1007
  359.     PDERR_NODEFAULTPRN = &H1008
  360.     PDERR_DNDMMISMATCH = &H1009
  361.     PDERR_CREATEICFAILURE = &H100A
  362.     PDERR_PRINTERNOTFOUND = &H100B
  363.     PDERR_DEFAULTDIFFERENT = &H100C
  364.  
  365.     CFERR_CHOOSEFONTCODES = &H2000
  366.     CFERR_NOFONTS = &H2001
  367.     CFERR_MAXLESSTHANMIN = &H2002
  368.  
  369.     FNERR_FILENAMECODES = &H3000
  370.     FNERR_SUBCLASSFAILURE = &H3001
  371.     FNERR_INVALIDFILENAME = &H3002
  372.     FNERR_BUFFERTOOSMALL = &H3003
  373.  
  374.     CCERR_CHOOSECOLORCODES = &H5000
  375. End Enum
  376.  
  377. ' Array of custom colors lasts for life of app
  378. Private alCustom(0 To 15) As Long, fNotFirst As Boolean
  379.  
  380. Public Enum EPrintRange
  381.     eprAll
  382.     eprPageNumbers
  383.     eprSelection
  384. End Enum
  385.  
  386. #If fComponent Then
  387. Private Sub Class_Initialize()
  388.     InitColors
  389. End Sub
  390. #End If
  391.  
  392. Function VBGetOpenFileName(FileName As String, _
  393.                            Optional FileTitle As String, _
  394.                            Optional FileMustExist As Boolean = True, _
  395.                            Optional MultiSelect As Boolean = False, _
  396.                            Optional ReadOnly As Boolean = False, _
  397.                            Optional HideReadOnly As Boolean = False, _
  398.                            Optional filter As String = "All (*.*)| *.*", _
  399.                            Optional FilterIndex As Long = 1, _
  400.                            Optional InitDir As String, _
  401.                            Optional DlgTitle As String, _
  402.                            Optional DefaultExt As String, _
  403.                            Optional Owner As Long = -1, _
  404.                            Optional Flags As Long = 0) As Boolean
  405.  
  406.     Dim opfile As OPENFILENAME, s As String, afFlags As Long
  407. With opfile
  408.     .lStructSize = Len(opfile)
  409.     
  410.     ' Add in specific flags and strip out non-VB flags
  411.     .Flags = (-FileMustExist * OFN_FILEMUSTEXIST) Or _
  412.              (-MultiSelect * OFN_ALLOWMULTISELECT) Or _
  413.              (-ReadOnly * OFN_READONLY) Or _
  414.              (-HideReadOnly * OFN_HIDEREADONLY) Or _
  415.              (Flags And CLng(Not (OFN_ENABLEHOOK Or _
  416.                                   OFN_ENABLETEMPLATE)))
  417.     ' Owner can take handle of owning window
  418.     If Owner <> -1 Then .hwndOwner = Owner
  419.     ' InitDir can take initial directory string
  420.     .lpstrInitialDir = InitDir
  421.     ' DefaultExt can take default extension
  422.     .lpstrDefExt = DefaultExt
  423.     ' DlgTitle can take dialog box title
  424.     .lpstrTitle = DlgTitle
  425.     
  426.     ' To make Windows-style filter, replace | and : with nulls
  427.     Dim ch As String, i As Integer
  428.     For i = 1 To Len(filter)
  429.         ch = Mid$(filter, i, 1)
  430.         If ch = "|" Or ch = ":" Then
  431.             s = s & vbNullChar
  432.         Else
  433.             s = s & ch
  434.         End If
  435.     Next
  436.     ' Put double null at end
  437.     s = s & vbNullChar & vbNullChar
  438.     .lpstrFilter = s
  439.     .nFilterIndex = FilterIndex
  440.  
  441.     ' Pad file and file title buffers to maximum path
  442.     s = FileName & String$(cMaxPath - Len(FileName), 0)
  443.     .lpstrFile = s
  444.     .nMaxFile = cMaxPath
  445.     s = FileTitle & String$(cMaxFile - Len(FileTitle), 0)
  446.     .lpstrFileTitle = s
  447.     .nMaxFileTitle = cMaxFile
  448.     ' All other fields set to zero
  449.     
  450.     If GetOpenFileName(opfile) Then
  451.         VBGetOpenFileName = True
  452.         FileName = MUtility.StrZToStr(.lpstrFile)
  453.         FileTitle = MUtility.StrZToStr(.lpstrFileTitle)
  454.         Flags = .Flags
  455.         ' Return the filter index
  456.         FilterIndex = .nFilterIndex
  457.         ' Look up the filter the user selected and return that
  458.         filter = FilterLookup(.lpstrFilter, FilterIndex)
  459.         If (.Flags And OFN_READONLY) Then ReadOnly = True
  460.     Else
  461.         VBGetOpenFileName = False
  462.         FileName = sEmpty
  463.         FileTitle = sEmpty
  464.         Flags = 0
  465.         FilterIndex = -1
  466.         filter = sEmpty
  467.     End If
  468. End With
  469. End Function
  470.  
  471. Function VBGetSaveFileName(FileName As String, _
  472.                            Optional FileTitle As String, _
  473.                            Optional OverWritePrompt As Boolean = True, _
  474.                            Optional filter As String = "All (*.*)| *.*", _
  475.                            Optional FilterIndex As Long = 1, _
  476.                            Optional InitDir As String, _
  477.                            Optional DlgTitle As String, _
  478.                            Optional DefaultExt As String, _
  479.                            Optional Owner As Long = -1, _
  480.                            Optional Flags As Long) As Boolean
  481.             
  482.     Dim opfile As OPENFILENAME, s As String
  483. With opfile
  484.     .lStructSize = Len(opfile)
  485.     
  486.     ' Add in specific flags and strip out non-VB flags
  487.     .Flags = (-OverWritePrompt * OFN_OVERWRITEPROMPT) Or _
  488.              OFN_HIDEREADONLY Or _
  489.              (Flags And CLng(Not (OFN_ENABLEHOOK Or _
  490.                                   OFN_ENABLETEMPLATE)))
  491.     ' Owner can take handle of owning window
  492.     If Owner <> -1 Then .hwndOwner = Owner
  493.     ' InitDir can take initial directory string
  494.     .lpstrInitialDir = InitDir
  495.     ' DefaultExt can take default extension
  496.     .lpstrDefExt = DefaultExt
  497.     ' DlgTitle can take dialog box title
  498.     .lpstrTitle = DlgTitle
  499.     
  500.     ' Make new filter with bars (|) replacing nulls and double null at end
  501.     Dim ch As String, i As Integer
  502.     For i = 1 To Len(filter)
  503.         ch = Mid$(filter, i, 1)
  504.         If ch = "|" Or ch = ":" Then
  505.             s = s & vbNullChar
  506.         Else
  507.             s = s & ch
  508.         End If
  509.     Next
  510.     ' Put double null at end
  511.     s = s & vbNullChar & vbNullChar
  512.     .lpstrFilter = s
  513.     .nFilterIndex = FilterIndex
  514.  
  515.     ' Pad file and file title buffers to maximum path
  516.     s = FileName & String$(cMaxPath - Len(FileName), 0)
  517.     .lpstrFile = s
  518.     .nMaxFile = cMaxPath
  519.     s = FileTitle & String$(cMaxFile - Len(FileTitle), 0)
  520.     .lpstrFileTitle = s
  521.     .nMaxFileTitle = cMaxFile
  522.     ' All other fields zero
  523.     
  524.     If GetSaveFileName(opfile) Then
  525.         VBGetSaveFileName = True
  526.         FileName = MUtility.StrZToStr(.lpstrFile)
  527.         FileTitle = MUtility.StrZToStr(.lpstrFileTitle)
  528.         Flags = .Flags
  529.         ' Return the filter index
  530.         FilterIndex = .nFilterIndex
  531.         ' Look up the filter the user selected and return that
  532.         filter = FilterLookup(.lpstrFilter, FilterIndex)
  533.     Else
  534.         VBGetSaveFileName = False
  535.         FileName = sEmpty
  536.         FileTitle = sEmpty
  537.         Flags = 0
  538.         FilterIndex = 0
  539.         filter = sEmpty
  540.     End If
  541. End With
  542. End Function
  543.  
  544. Private Function FilterLookup(ByVal sFilters As String, ByVal iCur As Long) As String
  545.     Dim iStart As Long, iEnd As Long, s As String
  546.     iStart = 1
  547.     If sFilters = sEmpty Then Exit Function
  548.     Do
  549.         ' Cut out both parts marked by null character
  550.         iEnd = InStr(iStart, sFilters, vbNullChar)
  551.         If iEnd = 0 Then Exit Function
  552.         iEnd = InStr(iEnd + 1, sFilters, vbNullChar)
  553.         If iEnd Then
  554.             s = Mid$(sFilters, iStart, iEnd - iStart)
  555.         Else
  556.             s = Mid$(sFilters, iStart)
  557.         End If
  558.         iStart = iEnd + 1
  559.         If iCur = 1 Then
  560.             FilterLookup = s
  561.             Exit Function
  562.         End If
  563.         iCur = iCur - 1
  564.     Loop While iCur
  565. End Function
  566.  
  567. Function VBGetFileTitle(sFile As String) As String
  568.     Dim sFileTitle As String, cFileTitle As Integer
  569.  
  570.     cFileTitle = cMaxPath
  571.     sFileTitle = String$(cMaxPath, 0)
  572.     cFileTitle = GetFileTitle(sFile, sFileTitle, cMaxPath)
  573.     If cFileTitle Then
  574.         VBGetFileTitle = sEmpty
  575.     Else
  576.         VBGetFileTitle = Left$(sFileTitle, InStr(sFileTitle, vbNullChar) - 1)
  577.     End If
  578.  
  579. End Function
  580.  
  581. ' ChooseColor wrapper
  582. Function VBChooseColor(Color As Long, _
  583.                        Optional AnyColor As Boolean = True, _
  584.                        Optional FullOpen As Boolean = False, _
  585.                        Optional DisableFullOpen As Boolean = False, _
  586.                        Optional Owner As Long = -1, _
  587.                        Optional Flags As Long) As Boolean
  588.  
  589.     Dim chclr As TCHOOSECOLOR
  590.     chclr.lStructSize = Len(chclr)
  591.     
  592.     ' Color must get reference variable to receive result
  593.     ' Flags can get reference variable or constant with bit flags
  594.     ' Owner can take handle of owning window
  595.     If Owner <> -1 Then chclr.hwndOwner = Owner
  596.  
  597.     ' Assign color (default uninitialized value of zero is good default)
  598.     chclr.rgbResult = Color
  599.  
  600.     ' Mask out unwanted bits
  601.     Dim afMask As Long
  602.     afMask = CLng(Not (CC_ENABLEHOOK Or _
  603.                        CC_ENABLETEMPLATE))
  604.     ' Pass in flags
  605.     chclr.Flags = afMask And (CC_RGBInit Or _
  606.                   IIf(AnyColor, CC_AnyColor, CC_SolidColor) Or _
  607.                   (-FullOpen * CC_FullOpen) Or _
  608.                   (-DisableFullOpen * CC_PreventFullOpen))
  609.  
  610.     ' If first time, initialize to white
  611.     If fNotFirst = False Then InitColors
  612.  
  613.     chclr.lpCustColors = VarPtr(alCustom(0))
  614.     ' All other fields zero
  615.     
  616.     If ChooseColor(chclr) Then
  617.         VBChooseColor = True
  618.         Color = chclr.rgbResult
  619.     Else
  620.         VBChooseColor = False
  621.         Color = -1
  622.     End If
  623.  
  624. End Function
  625.  
  626. Private Sub InitColors()
  627.     Dim i As Integer
  628.     ' Initialize with first 16 system interface colors
  629.     For i = 0 To 15
  630.         alCustom(i) = GetSysColor(i)
  631.     Next
  632.     fNotFirst = True
  633. End Sub
  634.  
  635. ' Property to read or modify custom colors (use to save colors in registry)
  636. Public Property Get CustomColor(i As Integer) As Long
  637.     ' If first time, initialize to white
  638.     If fNotFirst = False Then InitColors
  639.     If i >= 0 And i <= 15 Then
  640.         CustomColor = alCustom(i)
  641.     Else
  642.         CustomColor = -1
  643.     End If
  644. End Property
  645.  
  646. Public Property Let CustomColor(i As Integer, iValue As Long)
  647.     ' If first time, initialize to system colors
  648.     If fNotFirst = False Then InitColors
  649.     If i >= 0 And i <= 15 Then
  650.         alCustom(i) = iValue
  651.     End If
  652. End Property
  653.  
  654. ' ChooseFont wrapper
  655. Function VBChooseFont(CurFont As Font, _
  656.                       Optional PrinterDC As Long = -1, _
  657.                       Optional Owner As Long = -1, _
  658.                       Optional Color As Long = vbBlack, _
  659.                       Optional MinSize As Long = 0, _
  660.                       Optional MaxSize As Long = 0, _
  661.                       Optional Flags As Long = 0) As Boolean
  662.  
  663.     ' Unwanted Flags bits
  664.     Const CF_FontNotSupported = CF_Apply Or CF_EnableHook Or CF_EnableTemplate
  665.     
  666.     ' Flags can get reference variable or constant with bit flags
  667.     ' PrinterDC can take printer DC
  668.     If PrinterDC = -1 Then
  669.         PrinterDC = 0
  670.         If Flags And CF_PrinterFonts Then PrinterDC = Printer.hDC
  671.     Else
  672.         Flags = Flags Or CF_PrinterFonts
  673.     End If
  674.     ' Must have some fonts
  675.     If (Flags And CF_PrinterFonts) = 0 Then Flags = Flags Or CF_ScreenFonts
  676.     ' Color can take initial color, receive chosen color
  677.     If Color <> vbBlack Then Flags = Flags Or CF_EFFECTS
  678.     ' MinSize can be minimum size accepted
  679.     If MinSize Then Flags = Flags Or CF_LimitSize
  680.     ' MaxSize can be maximum size accepted
  681.     If MaxSize Then Flags = Flags Or CF_LimitSize
  682.  
  683.     ' Put in required internal flags and remove unsupported
  684.     Flags = (Flags Or CF_InitToLogFontStruct) And Not CF_FontNotSupported
  685.     
  686.     ' Initialize LOGFONT variable
  687.     Dim fnt As LOGFONT
  688.     Const PointsPerTwip = 1440 / 72
  689.     fnt.lfHeight = -(CurFont.Size * (PointsPerTwip / Screen.TwipsPerPixelY))
  690.     fnt.lfWeight = CurFont.Weight
  691.     fnt.lfItalic = CurFont.Italic
  692.     fnt.lfUnderline = CurFont.Underline
  693.     fnt.lfStrikeOut = CurFont.Strikethrough
  694.     ' Other fields zero
  695.     MBytes.StrToBytes fnt.lfFaceName, CurFont.Name
  696.  
  697.     ' Initialize TCHOOSEFONT variable
  698.     Dim cf As TCHOOSEFONT
  699.     cf.lStructSize = Len(cf)
  700.     If Owner <> -1 Then cf.hwndOwner = Owner
  701.     cf.hDC = PrinterDC
  702.     cf.lpLogFont = VarPtr(fnt)
  703.     cf.iPointSize = CurFont.Size * 10
  704.     cf.Flags = Flags
  705.     cf.rgbColors = Color
  706.     cf.nSizeMin = MinSize
  707.     cf.nSizeMax = MaxSize
  708.     
  709.     ' All other fields zero
  710.     
  711.     If ChooseFont(cf) Then
  712.         VBChooseFont = True
  713.         Flags = cf.Flags
  714.         Color = cf.rgbColors
  715.         CurFont.Bold = cf.nFontType And Bold_FontType
  716.         'CurFont.Italic = cf.nFontType And Italic_FontType
  717.         CurFont.Italic = fnt.lfItalic
  718.         CurFont.Strikethrough = fnt.lfStrikeOut
  719.         CurFont.Underline = fnt.lfUnderline
  720.         CurFont.Weight = fnt.lfWeight
  721.         CurFont.Size = cf.iPointSize / 10
  722.         CurFont.Name = MBytes.BytesToStr(fnt.lfFaceName)
  723.     Else
  724.         VBChooseFont = False
  725.     End If
  726.  
  727. End Function
  728.  
  729. ' PrintDlg wrapper
  730. Function VBPrintDlg(hDC As Long, _
  731.                     Optional PrintRange As EPrintRange = eprAll, _
  732.                     Optional DisablePageNumbers As Boolean, _
  733.                     Optional FromPage As Long = 1, _
  734.                     Optional ToPage As Long = &HFFFF, _
  735.                     Optional DisableSelection As Boolean, _
  736.                     Optional Copies As Integer, _
  737.                     Optional ShowPrintToFile As Boolean, _
  738.                     Optional DisablePrintToFile As Boolean = True, _
  739.                     Optional PrintToFile As Boolean, _
  740.                     Optional Collate As Boolean, _
  741.                     Optional PreventWarning As Boolean, _
  742.                     Optional Owner As Long, _
  743.                     Optional Printer As Object, _
  744.                     Optional Flags As Long) As Boolean
  745.     Dim afFlags As Long, afMask As Long
  746.     
  747.     ' Set PRINTDLG flags
  748.     afFlags = (-DisablePageNumbers * PD_NOPAGENUMS) Or _
  749.               (-DisablePrintToFile * PD_DISABLEPRINTTOFILE) Or _
  750.               (-DisableSelection * PD_NOSELECTION) Or _
  751.               (-PrintToFile * PD_PRINTTOFILE) Or _
  752.               (-(Not ShowPrintToFile) * PD_HIDEPRINTTOFILE) Or _
  753.               (-PreventWarning * PD_NOWARNING) Or _
  754.               (-Collate * PD_COLLATE) Or _
  755.               PD_USEDEVMODECOPIESANDCOLLATE Or _
  756.               PD_RETURNDC
  757.     If PrintRange = eprPageNumbers Then
  758.         afFlags = afFlags Or PD_PAGENUMS
  759.     ElseIf PrintRange = eprSelection Then
  760.         afFlags = afFlags Or PD_SELECTION
  761.     End If
  762.     ' Mask out unwanted bits
  763.     afMask = CLng(Not (PD_ENABLEPRINTHOOK Or _
  764.                        PD_ENABLEPRINTTEMPLATE))
  765.     afMask = afMask And _
  766.              CLng(Not (PD_ENABLESETUPHOOK Or _
  767.                        PD_ENABLESETUPTEMPLATE))
  768.     
  769.     ' Fill in PRINTDLG structure
  770.     Dim pd As TPRINTDLG
  771.     pd.lStructSize = Len(pd)
  772.     pd.hwndOwner = Owner
  773.     pd.Flags = afFlags And afMask
  774.     pd.nFromPage = FromPage
  775.     pd.nToPage = ToPage
  776.     pd.nMinPage = 1
  777.     pd.nMaxPage = &HFFFF
  778.     
  779.     ' Show Print dialog
  780.     If PrintDlg(pd) Then
  781.         VBPrintDlg = True
  782.         ' Return dialog values in parameters
  783.         hDC = pd.hDC
  784.         If (pd.Flags And PD_PAGENUMS) Then
  785.             PrintRange = eprPageNumbers
  786.         ElseIf (pd.Flags And PD_SELECTION) Then
  787.             PrintRange = eprSelection
  788.         Else
  789.             PrintRange = eprAll
  790.         End If
  791.         FromPage = pd.nFromPage
  792.         ToPage = pd.nToPage
  793.         PrintToFile = (pd.Flags And PD_PRINTTOFILE)
  794.         ' Get DEVMODE structure from PRINTDLG
  795.         Dim dvmode As DEVMODE, pDevMode As Long
  796.         pDevMode = GlobalLock(pd.hDevMode)
  797.         CopyMemory dvmode, ByVal pDevMode, Len(dvmode)
  798.         Call GlobalUnlock(pd.hDevMode)
  799.         ' Get Copies and Collate settings from DEVMODE structure
  800.         Copies = dvmode.dmCopies
  801.         Collate = (dvmode.dmCollate = DMCOLLATE_TRUE)
  802.         ' Set default printer properties
  803.         On Error Resume Next
  804.         Printer.Copies = Copies
  805.         Printer.Orientation = dvmode.dmOrientation
  806.         Printer.PaperSize = dvmode.dmPaperSize
  807.         Printer.PrintQuality = dvmode.dmPrintQuality
  808.     Else
  809.         VBPrintDlg = False
  810.     End If
  811.     
  812. End Function
  813.  
  814. ' PageSetupDlg wrapper
  815. Function VBPageSetupDlg(Optional Owner As Long, _
  816.                         Optional DisableMargins As Boolean, _
  817.                         Optional DisableOrientation As Boolean, _
  818.                         Optional DisablePaper As Boolean, _
  819.                         Optional DisablePrinter As Boolean, _
  820.                         Optional LeftMargin As Long, _
  821.                         Optional MinLeftMargin As Long, _
  822.                         Optional RightMargin As Long, _
  823.                         Optional MinRightMargin As Long, _
  824.                         Optional TopMargin As Long, _
  825.                         Optional MinTopMargin As Long, _
  826.                         Optional BottomMargin As Long, _
  827.                         Optional MinBottomMargin As Long, _
  828.                         Optional PaperSize As EPaperSize = epsLetter, _
  829.                         Optional Orientation As EOrientation = eoPortrait, _
  830.                         Optional PrintQuality As EPrintQuality = epqDraft, _
  831.                         Optional Units As EPageSetupUnits = epsuInches, _
  832.                         Optional Printer As Object, _
  833.                         Optional Flags As Long) As Boolean
  834.     Dim afFlags As Long, afMask As Long
  835.     ' Mask out unwanted bits
  836.     afMask = Not (PSD_EnablePagePaintHook Or _
  837.                   PSD_EnablePageSetupHook Or _
  838.                   PSD_EnablePageSetupTemplate)
  839.     ' Set TPAGESETUPDLG flags
  840.     afFlags = (-DisableMargins * PSD_DISABLEMARGINS) Or _
  841.               (-DisableOrientation * PSD_DISABLEORIENTATION) Or _
  842.               (-DisablePaper * PSD_DISABLEPAPER) Or _
  843.               (-DisablePrinter * PSD_DISABLEPRINTER) Or _
  844.               PSD_MARGINS Or PSD_MINMARGINS And afMask
  845.     Dim lUnits As Long
  846.     If Units = epsuInches Then
  847.         afFlags = afFlags Or PSD_INTHOUSANDTHSOFINCHES
  848.         lUnits = 1000
  849.     Else
  850.         afFlags = afFlags Or PSD_INHUNDREDTHSOFMILLIMETERS
  851.         lUnits = 100
  852.     End If
  853.     
  854.     Dim psd As TPAGESETUPDLG
  855.     ' Fill in PRINTDLG structure
  856.     psd.lStructSize = Len(psd)
  857.     psd.hwndOwner = Owner
  858.     psd.rtMargin.Top = TopMargin * lUnits
  859.     psd.rtMargin.Left = LeftMargin * lUnits
  860.     psd.rtMargin.bottom = BottomMargin * lUnits
  861.     psd.rtMargin.Right = RightMargin * lUnits
  862.     psd.rtMinMargin.Top = MinTopMargin * lUnits
  863.     psd.rtMinMargin.Left = MinLeftMargin * lUnits
  864.     psd.rtMinMargin.bottom = MinBottomMargin * lUnits
  865.     psd.rtMinMargin.Right = MinRightMargin * lUnits
  866.     psd.Flags = afFlags
  867.     
  868.     ' Show Print dialog
  869.     If PageSetupDlg(psd) Then
  870.         VBPageSetupDlg = True
  871.         ' Return dialog values in parameters
  872.         TopMargin = psd.rtMargin.Top / lUnits
  873.         LeftMargin = psd.rtMargin.Left / lUnits
  874.         BottomMargin = psd.rtMargin.bottom / lUnits
  875.         RightMargin = psd.rtMargin.Right / lUnits
  876.         MinTopMargin = psd.rtMinMargin.Top / lUnits
  877.         MinLeftMargin = psd.rtMinMargin.Left / lUnits
  878.         MinBottomMargin = psd.rtMinMargin.bottom / lUnits
  879.         MinRightMargin = psd.rtMinMargin.Right / lUnits
  880.         
  881.         ' Get DEVMODE structure from PRINTDLG
  882.         Dim dvmode As DEVMODE, pDevMode As Long
  883.         pDevMode = GlobalLock(psd.hDevMode)
  884.         CopyMemory dvmode, ByVal pDevMode, Len(dvmode)
  885.         Call GlobalUnlock(psd.hDevMode)
  886.         PaperSize = dvmode.dmPaperSize
  887.         Orientation = dvmode.dmOrientation
  888.         PrintQuality = dvmode.dmPrintQuality
  889.         ' Set default printer properties
  890.         On Error Resume Next
  891.         Printer.Copies = dvmode.dmCopies
  892.         Printer.Orientation = dvmode.dmOrientation
  893.         Printer.PaperSize = dvmode.dmPaperSize
  894.         Printer.PrintQuality = dvmode.dmPrintQuality
  895.     End If
  896.  
  897. End Function
  898.  
  899. #If fComponent = 0 Then
  900. Private Sub ErrRaise(e As Long)
  901.     Dim sText As String, sSource As String
  902.     If e > 1000 Then
  903.         sSource = App.ExeName & ".CommonDialog"
  904.         Select Case e
  905.         Case eeBaseCommonDialog
  906.             BugAssert True
  907.        ' Case ee...
  908.        '     Add additional errors
  909.         End Select
  910.         Err.Raise COMError(e), sSource, sText
  911.     Else
  912.         ' Raise standard Visual Basic error
  913.         sSource = App.ExeName & ".VBError"
  914.         Err.Raise e, sSource
  915.     End If
  916. End Sub
  917. #End If
  918.  
  919.